package com.lfs.market.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfs.market.exception.ServiceException;
import com.lfs.market.mapper.ProductCollectMapper;
import com.lfs.market.model.R;
import com.lfs.market.model.entity.ProductCollect;
import com.lfs.market.model.enums.ResultCode;
import com.lfs.market.service.ProductCollectService;
import org.springframework.stereotype.Service;

@Service
public class ProductCollectServiceImpl extends ServiceImpl<ProductCollectMapper, ProductCollect> implements ProductCollectService {
    @Override
    public R delete(String productId) {
        ProductCollect collect = lambdaQuery().eq(ProductCollect::getUserId, StpUtil.getLoginIdAsString())
                .eq(ProductCollect::getProductId, productId).one();
        if (null == collect) throw new ServiceException(ResultCode.NotFindError);
        if (!collect.getUserId().equals(StpUtil.getLoginIdAsString()))
            throw new ServiceException(ResultCode.NotFindError);
        boolean remove = lambdaUpdate()
                .eq(ProductCollect::getUserId, StpUtil.getLoginIdAsString())
                .eq(ProductCollect::getProductId, productId).remove();
        if (!remove) throw new ServiceException(ResultCode.DeleteError);
        return R.ok();
    }
}
